package net.easyjava.tools.page;



import net.easyjava.tools.BaseBean;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * Created by wxnacy on 2015/11/21.
 */
public class Page<T> extends BaseBean implements IPage<T> {
    private static final long serialVersionUID = 694766594366395961L;
    private long totalSize;//total rows size
    private int totalPage;//total page size
    private int page;//now page
    private int pageSize;//one rows size
    private Collection<T> rows; // data

    public Page() {
    }

    public Page(Collection<T> rows, long totalSize, int page) {
        setRows(rows);
        this.page = page;
        setTotalSize(totalSize);
    }

    public long getTotalSize() {
        return totalSize;
    }

    public void setTotalSize(long totalSize) {
        this.totalSize = totalSize;
        //compute this.totalPage by totalSize
        if(totalSize == 0){
            this.totalPage = 0;
        }else{
            this.totalPage = (int) ((this.totalSize / this.pageSize)+ ((this.totalSize % this.pageSize)==0?0:1));
        }
    }

    public int getTotalPage() {
        return totalPage;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public Collection<T> getRows() {
        return rows;
    }

    public void setRows(Collection<T> rows) {
        this.rows = rows;
        //compute this.pageSize by rows
        if(rows != null){
            this.pageSize = rows.size();
        }else{
            this.pageSize = 0;
        }
    }





    public int getFirstPage() {
        return 1;
    }

    public int getLastPage() {
        return this.totalPage;
    }

    public int getNextPage() {
        return this.page == this.totalPage?this.totalPage:this.page+1;
    }

    public int getPrevPage() {
        return this.page == getFirstPage()?getFirstPage():this.page-1;
    }

//    public boolean isFirst() {
//        return this.page == getFirstPage();
//    }
//
//    public boolean isLast() {
//        return this.page == this.totalPage;
//    }

    public static void main(String[] args) {
//        int totalSize = 13;
//        int pageSize = 4;
//
//        int totalPage;
//
//        totalPage = (totalSize / pageSize)+ ((totalSize % pageSize)==0?0:1);
//        System.out.println("totalPage = " + totalPage);
//
//        System.out.println("totalPage/pageSize = " + (totalSize / pageSize));
//        System.out.println("totalPage/pageSize = " + (totalSize % pageSize));

        List<String> list = Arrays.asList("1", "2", "3", "4", "5", "6", "7");
        Page<String> page = new Page<String>(list,22,4);
        System.out.println("page.toString() = " + page.toString());
        System.out.println("page.getFirstPage() = " + page.getFirstPage());
        System.out.println("page.getLastPage() = " + page.getLastPage());
        System.out.println("page.getNextPage() = " + page.getNextPage());
        System.out.println("page.getPrevPage() = " + page.getPrevPage());
//        System.out.println("page.isFirstPage() = " + page.isFirstPage());
//        System.out.println("page.isLastPage() = " + page.isLastPage());

    }
}
